import requests
import json
import pandas as pd
import os
from datetime import datetime

# 定义请求的 URL
base_url = "http://36.111.148.232:18990/fitness4DutyStatus/topic5/api/simple/"


'''
# 按类型分类的 indexNames
index_names = {
    "citybus": [
        "citybus.prejob",
        "citybus.outtowork",
        "citybus.onduty",
        "citybus.driverinfo",
        "citybus.enterpriseinfo",
        "citybus.deviceinfo"
    ],
    "railtransit": [
        "railtransit.prejob",
        "railtransit.outtowork",
        "railtransit.onduty",
        "railtransit.driverinfo",
        "railtransit.enterpriseinfo",
        "railtransit.deviceinfo"
    ],
    "passengerferry": [
        "passengerferry.prejob",
        "passengerferry.outtowork",
        "passengerferry.onduty",
        "passengerferry.driverinfo",
        "passengerferry.enterpriseinfo",
        "passengerferry.deviceinfo"
    ],
    "twoguestacrisis": [
        "twoguestacrisis.prejob",
        "twoguestacrisis.outtowork",
        "twoguestacrisis.onduty",
        "twoguestacrisis.driverinfo",
        "twoguestacrisis.enterpriseinfo",
        "twoguestacrisis.deviceinfo"
    ],
    "chongqing": [
        "chongqing_data_dongtaijiankongdangan",
        "chongqing_data_weifaweigui",
        "chongqing_data_jiashiyuan",
        "chongqing_twoguestacrisis.enterpriseinfo"
    ]
}
'''


# 按类型分类的 indexNames
index_names = {
    "railtransit": [
        "railtransit.prejob"
    ]
}


# 获取当前时间并格式化为字符串
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
output_dir = f"./output_{timestamp}"  # 设置输出文件夹的名称
os.makedirs(output_dir, exist_ok=True)

# 遍历每个类别及其 indexNames
for category, names in index_names.items():
    # 创建类别文件夹以保存 Excel 文件
    category_dir = os.path.join(output_dir, category)
    os.makedirs(category_dir, exist_ok=True)

    for index_name in names:
        url = f"{base_url}{index_name}/list"  # 更新 URL
        current_page = 1
        all_results = []  # 用于保存所有页的数据
        max_pages = 5  # 最大页数限制

        while current_page <= max_pages:
            # 定义请求体（JSON 格式）
            data = {
                "pager": {
                    "pageSize": 100,
                    "currentPage": current_page
                }
            }

            try:
                # 发送 POST 请求
                response = requests.post(url, json=data)
                response.raise_for_status()  # 检查请求是否成功

                # 解析 JSON 响应内容
                response_data = response.json()
                print(f"完整响应数据 for {index_name} - Page {current_page}:")
                print(json.dumps(response_data, ensure_ascii=False, indent=4))

                # 检查 data 字段的结构
                if 'data' in response_data:
                    data_content = response_data['data']

                    # 根据实际情况确定数据结构
                    if isinstance(data_content, list):
                        all_results.extend(data_content)  # 添加当前页的数据
                    elif isinstance(data_content, dict) and 'result' in data_content:
                        all_results.extend(data_content['result'])
                    else:
                        print(f"错误：'{index_name}' 的 'data' 字段不是列表或未包含 'result' 字段")
                        break  # 退出循环

                    # 只要有数据返回，就继续请求下一页
                    if len(data_content) < 100:  # 如果当前页数据少于 pageSize，说明没有更多数据
                        break
                else:
                    print("错误：未找到 'data' 字段")
                    break

            except requests.exceptions.HTTPError as http_err:
                print(f"HTTP 错误发生: {http_err} for {index_name} - Page {current_page}")
                break
            except requests.exceptions.RequestException as req_err:
                print(f"请求错误发生: {req_err} for {index_name} - Page {current_page}")
                break
            except ValueError as json_err:
                print(f"JSON 解析错误: {json_err} for {index_name} - Page {current_page}")
                break
            except Exception as e:
                print(f"发生了一个错误: {e} for {index_name} - Page {current_page}")
                break

            current_page += 1  # 增加页码以获取下一页

        # 创建 DataFrame
        if all_results:
            df = pd.DataFrame(all_results)

            # 将 DataFrame 保存为 Excel 文件
            excel_filename = os.path.join(category_dir, f"{index_name}.xlsx")  # 保存到对应文件夹
            df.to_excel(excel_filename, index=False)  # 移除 encoding 参数
            print(f"数据已成功保存为 {excel_filename}")
        else:
            print(f"'{index_name}' 没有有效数据，已跳过此索引。")